package com.qf.listeners;

import com.qf.cache.GoodTypeCache;
import com.qf.pojo.TbGoodsType;
import com.qf.service.GoodsTypeService;
import com.qf.service.Impl.GoodsTypeServiceImpl;
import com.qf.utils.ExcutorsUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Executors;

@WebListener
public class LoadTypeCacheContextListener implements ServletContextListener {
   private GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
    @Override
    public void contextInitialized(ServletContextEvent sce) {

        try {
            System.out.println("加载分类缓存");
            List<TbGoodsType> goodsTypeList = goodsTypeService.findAllSecondLevelType();
            GoodTypeCache.setAllSecondTypes(goodsTypeList);
            sce.getServletContext().setAttribute("goodsTypeList", GoodTypeCache.getAllSecondTypes());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void contextDestroyed(ServletContextEvent sce){
        GoodTypeCache.clearCache();
    }
}
